package cn.wildfire.chat.app.main;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by HEKE, 2019/8/24 16:33
 * TODO 推送消息界面
 */
public class PushMessageActivity extends Activity {

    @BindView(R2.id.messageIcon)
    ImageView messageIcon;
    @BindView(R2.id.messageTitleText)
    TextView messageTitleText;
    @BindView(R2.id.messageText)
    TextView messageText;

    private void initViews() {
        Intent intent = getIntent();
        if (intent != null) {
            String msg = intent.getStringExtra("msg");
            messageText.setText("" + msg);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        setContentView(R.layout.activity_push_message);
        ButterKnife.bind(this);
        initViews();
    }

    /**
     * 这个方法是当这个activity没有销毁的时候，人为的按下锁屏键，然后再启动这个Activity的时候会去调用
     *
     * @param intent
     */
    @Override
    protected void onNewIntent(Intent intent) {
        try {
            PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
            if (!pm.isScreenOn()) {
                String msg = intent.getStringExtra("msg");
                messageText.setText("又收到消息:" + msg);
                //点亮屏幕
                @SuppressLint("InvalidWakeLockTag")
                PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
                        PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
                wl.acquire();
                wl.release();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
